﻿using System;
using System.Linq;
using WebReferenceManager;
using WebReferenceManager.ServiceReference1;
using WindowSystem;

namespace WindowsGame
{
    public sealed class ConnectToServer : Window
    {
        public WebReferenceManager.ServerConnection ServerConnection;
        private Game1 game;
        private GUIManager gui;
        private TextBox ServerAddress;
        private ListBox ServerAddresses;

        public ConnectToServer(Game1 game, GUIManager gui, WebReferenceManager.ServerConnection serverConnection) : base(game, gui)
        {
            this.game = game;
            this.gui = gui;
            ServerConnection = serverConnection;

            const int width = 440;
            const int height = 350;

            TitleText = "Connect to Server";
            Width = width;
            Height = height;
            gui.Add(this);

            Add(new Label(game, gui) { Text = "Server", X = 10, Y = 15, Width = 60, Height = 20, });
            Add(ServerAddress = new TextBox(game, gui) { X = 60, Y = 10, Width = 350, });

            Add(new Label(game, gui) { Text = "Server List", X = 10, Y = ServerAddress.Y + 35, Width = 100, Height = 20, });
            Add(ServerAddresses = new ListBox(game, gui) { X = 10, Y = ServerAddress.Y + 60, Width = 400, Height = 200 });
            ServerAddresses.AddEntry("http://localhost:2509/Service1.svc");
            ServerAddresses.AddEntry("http://ledpup.dyns.net/xstratserver/service1.svc");
            ServerAddresses.AddEntry("http://ledlaptop/WcfService1/Service1.svc");
        

            //ServerAddresses.SelectedIndex = 0;
            
            ServerAddresses.SelectedChanged += new SelectionChangedHandler(ServerAddresses_SelectedChanged);

            if (serverConnection != null)
            {
                var currentServerAddress = serverConnection.WebService.Endpoint.Address.ToString();
                var currentServerAddressInList = ServerAddresses.Items.SingleOrDefault(o => o.Text == currentServerAddress) != null;

                if (!currentServerAddressInList)
                    ServerAddresses.AddEntry(currentServerAddress);

                ServerAddresses.SelectedIndex = ServerAddresses.Items.Single(o => o.Text == currentServerAddress).Key;
            }

            var btnConnect = new TextButton(game, gui) { Text = "Connect", X = 5, Y = height - 65, };
            btnConnect.Click += (btnConnect_Click);
            Add(btnConnect);

            CenterWindow();
        }

        void ServerAddresses_SelectedChanged(UIComponent sender)
        {
            ServerAddress.Text = ServerAddresses.SelectedText;
        }


        void btnConnect_Click(UIComponent sender)
        {
            try
            {
                ServerConnection = new ServerConnection("WSHttpBinding_IService1", ServerAddresses.SelectedText);
                CloseWindow();
            }
            catch (Exception ex)
            {
                (new MessageBox(game, gui, ex.Message, "Cannot connect to server", MessageBoxButtons.OK, MessageBoxType.Error)).Show(true);
            }
        }
    }
}